﻿using System;
using NLog;
using System.Configuration;

namespace V.Utility.VLogger
{
    /// <summary>
    /// 封装NLog记录文本日志的方法
    /// <para>提供了记录日志产生的位置，异常信息及格式化输出的能力</para>
    /// </summary>
    /// 创建人：
    /// 创建日期：
    public class TextLogger
    {
        private static readonly Logger _log = LogManager.GetCurrentClassLogger();
        private readonly string _logger;

        public TextLogger(string logger)
        {
            _logger = logger;
        }
        /// <summary>
        /// 记录错误信息日志
        /// </summary>
        public void Info(string format,params object[] args) { WriteLog(LogLevel.Info,null,format,args); }
        /// <summary>
        /// 记录错误信息日志
        /// </summary>
        public void Error(string format, params object[] args) { WriteLog(LogLevel.Error, null, format, args); }
        /// <summary>
        /// 记录错误信息日志
        /// </summary>
        public void Error(Exception ex,string format, params object[] args) { WriteLog(LogLevel.Error, ex, format, args); }
        /// <summary>
        /// 记录错误信息日志
        /// </summary>
        public void Warn(string format, params object[] args) { WriteLog(LogLevel.Warn, null, format, args); }
        /// <summary>
        /// 记录错误信息日志
        /// </summary>
        public void Warn(Exception ex, string format, params object[] args) { WriteLog(LogLevel.Warn, ex, format, args); }
        /// <summary>
        /// 记录错误信息日志
        /// </summary>
        /// <param name="format"></param>
        /// <param name="args"></param>
        public void Trace(string format, params object[] args) { WriteLog(LogLevel.Trace, null, format, args); }
        /// <summary>
        /// 记录错误信息日志
        /// </summary>
        public void Trace(Exception ex, string format, params object[] args) { WriteLog(LogLevel.Trace, ex, format, args); }

        private void WriteLog(LogLevel level,Exception ex,string format,params object[] args)
        {
            string message = string.Format(format ?? string.Empty, args);
            if (ex != null) message = string.Concat(message, "\r\n", ex.Message);
            message = string.Concat(_logger, "\r\n", message);
            message = string.Concat(message, "\r\n--------------------------------------------------------\r\n");
            _log.Log(level,message);
            
        }
    }
}
